﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
using System.Drawing;
using System.Drawing.Drawing2D;

namespace Paltop_v0._1.UI
{
    /// <summary>
    /// This handler change the size of an image and write it in a memory stream
    /// </summary>
    public class ThumbnailHandler : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            string url = context.Request.QueryString["imageUrl"];
            string extension = context.Request.QueryString["ext"];
            int maxWidth = 350;
            int maxHeight = 300;
            string maxH = context.Request.QueryString["maxH"];
            if (maxH != null)
            {
                maxHeight = Convert.ToInt32(maxH);
            }
            string maxW = context.Request.QueryString["maxW"];
            if (maxW != null)
            {
                maxWidth = Convert.ToInt32(maxW);
            }
            string path = HttpContext.Current.Server.MapPath("/"+url);
            System.Drawing.Image img = System.Drawing.Image.FromFile(path);
            Bitmap srcBmp = new Bitmap(path);
            extension = extension.ToLower();
            try
            {
                int width = img.Size.Width;
                int height = img.Size.Height;

                bool doWidthResize = (maxWidth > 0 && width > maxWidth && width > height);
                bool doHeightResize = (maxHeight > 0 && height > maxHeight && height > width);

                //only resize if the image is bigger than the max
                if (doWidthResize || doHeightResize)
                {
                    int iStart;
                    Decimal divider;
                    if (doWidthResize)
                    {
                        iStart = width;
                        divider = Math.Abs((Decimal)iStart / (Decimal)maxWidth);
                        width = maxWidth;
                        height = (int)Math.Round((Decimal)(height / divider));
                    }
                    else
                    {
                        iStart = height;
                        divider = Math.Abs((Decimal)iStart / (Decimal)maxHeight);
                        height = maxHeight;
                        width = (int)Math.Round((Decimal)(width / divider));
                    }
                }
                    Bitmap target = new Bitmap(width, height);
                    using (Graphics graphics = Graphics.FromImage(target))
                    {
                        graphics.CompositingQuality = CompositingQuality.HighSpeed;
                        graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
                        graphics.CompositingMode = CompositingMode.SourceOver;
                        graphics.Clear(Color.Transparent);
                        graphics.DrawImage(srcBmp, 0, 0, width, height);
                        try
                        {
                            using (MemoryStream ms = new MemoryStream())
                            {
                                if (extension.IndexOf("jpg") > -1)
                                {
                                    target.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
                                }
                                else if (extension.IndexOf("png") > -1)
                                {
                                    target.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
                                }
                                else if (extension.IndexOf("gif") > -1)
                                {
                                    target.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
                                }
                                else // if (extension.IndexOf("bmp") > -1)
                                {
                                    target.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);
                                }
                                context.Response.Clear();
                                context.Response.ContentType = "image/" + extension;
                                ms.WriteTo(context.Response.OutputStream);

                                context.Response.End();
                            }
                        }
                        finally
                        {
                            srcBmp.Dispose();
                        }
                    }
                
            }
            finally
            {
                img.Dispose();
            }

            context.Response.End();
        }
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }

}